//  Copyright © 2014 Allen County Public Library.

//        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.

package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import com.google.analytics.tracking.android.EasyTracker;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;
import us.in.lib.acpl.Mobile_ACPL.models.BestSellerDisplayInfo;
import us.in.lib.acpl.Mobile_ACPL.models.BestSellerListsResponse;
import us.in.lib.acpl.Mobile_ACPL.models.RecommendedReadingDisplayInfo;
import us.in.lib.acpl.Mobile_ACPL.models.RecommendedReadingListsResponse;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseActivity;
import us.in.lib.acpl.Mobile_ACPL.rest.SirsiClient;
import us.in.lib.acpl.Mobile_ACPL.ws.MainSearchHandler;
import us.in.lib.acpl.Mobile_ACPL.ws.SearchCatalogHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@EActivity(R.layout.featured_titles)
public class FeaturedTitlesActivity extends BaseActivity {

	
	@RestService
	SirsiClient client;
	
	@ViewById
	StickyListHeadersListView listView;
	
	
	
	List<BestSellerDisplayInfo> bestsellerLists;
	List<RecommendedReadingDisplayInfo> recLists;
	BestsellerAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}
	
	@AfterViews
	public void afterViews(){
		this.getActionBar().setTitle("   Featured Titles");
		bestsellerLists = new ArrayList<BestSellerDisplayInfo>();
		recLists = new ArrayList<RecommendedReadingDisplayInfo>();
		adapter = new BestsellerAdapter();
		listView.setAdapter(adapter);
		getLists();
		getRecLists();
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position<bestsellerLists.size()){
					BestSellerDisplayInfo list = bestsellerLists.get(position);
					Intent intent = new Intent(parent.getContext(),BestSellerListActivity_.class);
					intent.putExtra("listId", list.bestSellerDisplayID);
					startActivity(intent);
				}else{
					RecommendedReadingDisplayInfo list = recLists.get(position=bestsellerLists.size());
					Intent intent = new Intent(parent.getContext(),RecommendedReadingListActivity_.class);
					intent.putExtra("listId", list.recommendedReadingDisplayID);
					startActivity(intent);
				}
				
			}
			
		});
	}
	
	@Background
	public void getRecLists(){
		RecommendedReadingListsResponse recLists = client.getRecommendedReadingLists();
		getRecListsComplete(recLists.recommendedReadingDisplayInfo);
	}
	
	@UiThread
	public void getRecListsComplete(List<RecommendedReadingDisplayInfo> recLists){
		ArrayList<RecommendedReadingDisplayInfo> toRemove = new ArrayList<RecommendedReadingDisplayInfo>();
		for(RecommendedReadingDisplayInfo list: recLists){
			if(list.description.contains("Children's")||list.description.contains("Cookbook")){
				toRemove.add(list);
			}
		}
		for(RecommendedReadingDisplayInfo list: toRemove){
			recLists.remove(list);
		}
		this.recLists = recLists;
		adapter.notifyDataSetChanged();
	}
	
	@Background
	public void getLists(){
		BestSellerListsResponse lists = client.getBestsellerLists();
		getListsComplete(lists.bestSellerDisplayInfo);
	}
	
	@UiThread
	public void getListsComplete(List<BestSellerDisplayInfo> lists){
		ArrayList<BestSellerDisplayInfo> toRemove = new ArrayList<BestSellerDisplayInfo>();
		for(BestSellerDisplayInfo list: lists){
			if(list.description.contains("NO LONGER MAINTAINED")){
				toRemove.add(list);
			}
		}
		for(BestSellerDisplayInfo list: toRemove){
			lists.remove(list);
		}
		bestsellerLists = lists;
		adapter.notifyDataSetChanged();
		
		
	}
	
	

	public class BestsellerAdapter extends BaseAdapter implements StickyListHeadersAdapter{


	

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return bestsellerLists.size()+recLists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			if(position<bestsellerLists.size()){
				return bestsellerLists.get(position);
			}else{
				return recLists.get(position-bestsellerLists.size());
			}
			
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
			convertView = inflater.inflate(R.layout.featured_titles_list_item, null);
			
			TextView tv = (TextView)convertView.findViewById(R.id.listTitle);
			if(position<bestsellerLists.size()){
				BestSellerDisplayInfo list = bestsellerLists.get(position);
				tv.setText(list.description);
			}else{
				RecommendedReadingDisplayInfo list = recLists.get(position-bestsellerLists.size());
				tv.setText(list.description);
			}
			
			
			return convertView;
		}

		@Override
		public View getHeaderView(int position, View convertView,
				ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
			convertView = inflater.inflate(R.layout.featured_titles_header, null);
			
			TextView tv = (TextView)convertView.findViewById(R.id.headerText);
			
			if(position<bestsellerLists.size()){
				tv.setText("Bestseller Lists");
			}else{
				tv.setText("Recommended Reading Lists");
			}
			
			return convertView;
		}

		@Override
		public long getHeaderId(int position) {
			// TODO Auto-generated method stub
			if(position<bestsellerLists.size()){
				return 0;
			}
			
			else{
				return 1;
			}
		}

		
	}
}
